#!/bin/sh /etc/rc.common
#
# Copyright (C) 2015 OpenWrt-dist
#
# This is free software, licensed under the GNU General Public License v3.
# See /LICENSE for more information.
#

START=30

CONFIG=mia

uci_get_by_type() {
	local index=0
	if [ -n $4 ]; then
		index=$4
	fi
	local ret=$(uci get $CONFIG.@$1[$index].$2 2>/dev/null)
	echo ${ret:=$3}
}

add_rule(){
	nft insert rule inet fw4 dstnat ip protocol tcp tcp dport 53 counter jump REDRECT_MIA
	nft insert rule inet fw4 dstnat ip protocol udp udp dport 53 counter jump REDRECT_MIA
	nft add rule inet fw4 REDRECT_MIA counter redrect to :53
	nft insert rule inet fw4 input_lan udp dport 53 counter jump MIA
	nft insert rule inet fw4 input_lan tcp dport 53 counter jump MIA
	strict=$(uci get mia.@basic[0].strict)
	[ $strict -eq 1 ] && nft insert rule inet fw4 forward_lan counter jump MIA
	for i in $(seq 0 100)
	do
		local enable=$(uci_get_by_type macbind enable '' $i)
		local macaddr=$(uci_get_by_type macbind macaddr '' $i)
		local timeon=$(uci_get_by_type macbind timeon '' $i)
		local timeoff=$(uci_get_by_type macbind timeoff '' $i)
		local z1=$(uci_get_by_type macbind z1 '' $i)
		local z2=$(uci_get_by_type macbind z2 '' $i)
		local z3=$(uci_get_by_type macbind z3 '' $i)
		local z4=$(uci_get_by_type macbind z4 '' $i)
		local z5=$(uci_get_by_type macbind z5 '' $i)
		local z6=$(uci_get_by_type macbind z6 '' $i)
		local z7=$(uci_get_by_type macbind z7 '' $i)
        	[ "$z1" == "1" ] && Z1="Monday,"
        	[ "$z2" == "1" ] && Z2="Tuesday,"
        	[ "$z3" == "1" ] && Z3="Wednesday,"
        	[ "$z4" == "1" ] && Z4="Thursday,"
        	[ "$z5" == "1" ] && Z5="Friday,"
        	[ "$z6" == "1" ] && Z6="Saturday,"
        	[ "$z7" == "1" ] && Z7="Sunday"
        	if [ -z $enable ] || [ -z $macaddr ] || [ -z $timeoff ] || [ -z $timeon ]; then
                	break
        	fi
       		if [ "$enable" == "1" ]; then
                	nft insert rule inet fw4 MIA meta l4proto tcp ether saddr $macaddr meta hour $timeon-$timeoff meta day { $Z1$Z2$Z3$Z4$Z5$Z6$Z7 } counter jump reject_to_wan
                	nft insert rule inet fw4 MIA meta l4proto udp ether saddr $macaddr meta hour $timeon-$timeoff meta day { $Z1$Z2$Z3$Z4$Z5$Z6$Z7 } counter jump reject_to_wan
		fi
		for n in $(seq 1 7)
		do
			unset "Z$n"
		done
	done
}

del_rule(){
        for MIA_CHAIN in input_lan dstnat forward_lan
        do
                if [ ${MIA_CHAIN} == "dstnat" ]; then
                        handles=$(nft -a list chain inet fw4 ${MIA_CHAIN} | grep -E "REDRECT_MIA" | awk -F '# handle ' '{print$2}')
                else
                        handles=$(nft -a list chain inet fw4 ${MIA_CHAIN} | grep -E "MIA" | awk -F '# handle ' '{print$2}')
                fi
                for handle in $handles
                do
                        nft delete rule inet fw4 ${MIA_CHAIN} handle ${handle} 2>/dev/null
                done
        done
}
 
start(){
        stop
        enable=$(uci get mia.@basic[0].enable)
        [ $enable -eq 0 ] && exit 0
        nft add chain inet fw4 MIA
        nft add chain inet fw4 REDRECT_MIA
        add_rule
}
stop(){
        del_rule
        nft flush chain inet fw4 REDRECT_MIA 2>/dev/null
        nft delete chain inet fw4 REDRECT_MIA 2>/dev/null
        nft flush chain inet fw4 MIA 2>/dev/null
        nft delete chain inet fw4 MIA 2>/dev/null
}
